package com.ruoyi.qiushi.service.equipment.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.qiushi.domain.equipment.QiushiDevice;
import com.ruoyi.qiushi.domain.equipment.QiushiDeviceIn;
import com.ruoyi.qiushi.mapper.equipment.QiushiDeviceInMapper;
import com.ruoyi.qiushi.mapper.equipment.QiushiDeviceMapper;
import com.ruoyi.qiushi.service.equipment.IQiushiDeviceInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 设备入库Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-22
 */
@Service
public class QiushiDeviceInServiceImpl implements IQiushiDeviceInService {
    @Autowired
    private QiushiDeviceInMapper qiushiDeviceInMapper;
    @Autowired
    private QiushiDeviceMapper qiushiDeviceMapper;

    /**
     * 查询设备入库
     *
     * @param id 设备入库主键
     * @return 设备入库
     */
    @Override
    public QiushiDeviceIn selectQiushiDeviceInById(Long id) {
        return qiushiDeviceInMapper.selectQiushiDeviceInById(id);
    }

    /**
     * 查询设备入库列表
     *
     * @param qiushiDeviceIn 设备入库
     * @return 设备入库
     */
    @Override
    public List<QiushiDeviceIn> selectQiushiDeviceInList(QiushiDeviceIn qiushiDeviceIn) {
        return qiushiDeviceInMapper.selectQiushiDeviceInList(qiushiDeviceIn);
    }

    /**
     * 新增设备入库
     *
     * @param qiushiDeviceIn 设备入库
     * @return 结果
     */
    @Override
    public String insertQiushiDeviceIn(QiushiDeviceIn qiushiDeviceIn) {
        qiushiDeviceIn.setCreateTime(DateUtils.getNowDate());
        qiushiDeviceIn.setOperatingUserId(SecurityUtils.getUserId());
        qiushiDeviceIn.setOperatingUserName(SecurityUtils.getUsername());

        StringBuilder res = new StringBuilder("设备编号: ");
        boolean flag = true;

        for (List<String> list : qiushiDeviceIn.getDeviceList()) {
            QiushiDevice temp = qiushiDeviceMapper.selectQiushiDeviceByDeviceNo(list.get(0));

            if (temp == null) {
                QiushiDevice qiushiDevice = new QiushiDevice();

                qiushiDevice.setInNo(qiushiDeviceIn.getInNo());
                qiushiDevice.setDeviceType(qiushiDeviceIn.getDeviceType());

                qiushiDevice.setDeviceNo(list.get(0));
                qiushiDevice.setDeviceName(list.get(1));
                qiushiDevice.setSpec(list.get(2));

                qiushiDevice.setCreateTime(new Date());
                qiushiDevice.setUpdateTime(new Date());
                if (flag) qiushiDeviceMapper.insertQiushiDevice(qiushiDevice);
            } else {
                res.append(list.get(0)).append(" ");
                flag = false;
            }
        }

        res.append("重复入库");

        qiushiDeviceInMapper.insertQiushiDeviceIn(qiushiDeviceIn);
        return flag ? "1" : res.toString();
    }

    /**
     * 修改设备入库
     *
     * @param qiushiDeviceIn 设备入库
     * @return 结果
     */
    @Override
    public int updateQiushiDeviceIn(QiushiDeviceIn qiushiDeviceIn) {
        qiushiDeviceIn.setUpdateTime(DateUtils.getNowDate());
        qiushiDeviceIn.setOperatingUserId(SecurityUtils.getUserId());
        qiushiDeviceIn.setOperatingUserName(SecurityUtils.getUsername());

        return qiushiDeviceInMapper.updateQiushiDeviceIn(qiushiDeviceIn);
    }

    /**
     * 删除设备入库信息
     *
     * @param id 设备入库主键
     * @return 结果
     */
    @Override
    public int deleteQiushiDeviceInById(Long id) {
        return qiushiDeviceInMapper.deleteQiushiDeviceInById(id);
    }
}
